package com.sgb.member.ui.adapter

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.sgb.member.R
import com.sgb.member.databinding.LayoutNewAddItemGrayBinding


import com.sgb.member.entity.ParamDtosEntity
import com.swgk.core.util.MToast

class NewAddAdapter(val context: Context, var data: MutableList<ParamDtosEntity>) : RecyclerView.Adapter<NewAddAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding: LayoutNewAddItemGrayBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context),
                R.layout.layout_new_add_item_gray, parent, false);
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        data[position].run {
            holder.binding.bean = this
            holder.binding.ivDel.setOnClickListener {
                data.removeAt(position)
                notifyDataSetChanged()
            }
        }
        holder.binding.tvDescContent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

            override fun afterTextChanged(s: Editable?) {
                var len = s?.toString()?.trim()?.length
                holder.binding.tvNum.text = "$len/300"
            }
        })
        holder.binding.executePendingBindings()
    }

    override fun getItemCount() = data.size


    fun addData() {
        data.let {
            if (it.size < 5) {
                if (it.size != 0) {
                    val entity = it[it.size-1]
                    if (TextUtils.isEmpty(entity.title)) {
                        MToast.showToast(context, "请输入标题")
                        return
                    }
                    if (TextUtils.isEmpty(entity.content)) {
                        MToast.showToast(context, "请输入内容")
                        return
                    }
                    data.add(ParamDtosEntity("",""))
                    notifyDataSetChanged()
                } else {
                    data.add(ParamDtosEntity("",""))
                    notifyDataSetChanged()
                }
            } else {
                MToast.showToast(context, "最多新增5次")
            }
        }
    }


    //提交验证
    fun submitYanZheng(): Boolean {
        data.let {
            if (it.size != 0) {
                for (entity in data) {
                    if (TextUtils.isEmpty(entity.title) && TextUtils.isEmpty(entity.content)) {//都为空也可以提交

                    } else {
                        if (TextUtils.isEmpty(entity.title)) {
                            MToast.showToast(context, "请输入标题")
                            return false
                        }
                        if (TextUtils.isEmpty(entity.content)) {
                            MToast.showToast(context, "请输入内容")
                            return false
                        }
                    }
                }
            }
            return true
        }
    }

    inner class ViewHolder(val binding: LayoutNewAddItemGrayBinding) : RecyclerView.ViewHolder(binding.root)
}
